package com.sepro.service.impl;

import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Service;

import com.sepro.dao.AdminDao;
import com.sepro.entity.Admin;
import com.sepro.entity.Member;
import com.sepro.exception.MemberNotExistException;
import com.sepro.exception.PasswordMatchFailedException;
import com.sepro.service.AdminService;

@Service("adminServiceImpl")
public class AdminServiceImpl extends BaseServiceImpl<Admin, String> implements
		AdminService {

	@Resource(name = "adminDaoImpl")
	private AdminDao adminDao;
	
	@Resource(name = "adminDaoImpl")
	public void setBaseDao(AdminDao adminDao){
		super.setBaseDao(adminDao);
	}
	
	
	public Admin verifyAdmin(String username, String password) 
		throws PasswordMatchFailedException, MemberNotExistException{
		
		List<Admin> allList = getAllList();
		Iterator<Admin> iter = allList.iterator();
		while(iter.hasNext()){
			Admin admin = iter.next();
			String enPassword = DigestUtils.md5Hex(password);
			if(username.equals(admin.getUsername())){
				if(admin.getPassword().equals(enPassword)){
					//找到该管理员的匹配信息
					return admin;
				}else{
					//密码不匹配
					throw new PasswordMatchFailedException();
				}
			}
		}
		throw new MemberNotExistException();
	}
}
